package com.bestcem.xm.member.controller.v2;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.member.service.SequenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 示例服务
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/8/4
 */
@Slf4j
@RestController
@RequestMapping("member/v2")
public class SequenceController {

    @Resource
    private SequenceService sequenceService;

    /**
     * 获取名称序号（标签组，标签）
     *
     * @param name
     * @return
     */
    @GetMapping("/{name}/sequence")
    public XmResultJson getSequence(@PathVariable String name, HttpServletRequest request) {
        
        String orgId = SecurityContextHolder.getOrgId();
        // 获取序号
        ServiceResult<Integer> result = sequenceService.getNameSequence(name, orgId);
        if (result.isSuccess()) {
            JSONObject object = new JSONObject();
            object.put("sequence", result.getData());
            return XmResultJson.success(object);
        } else {
            return XmResultJson.fail(-1, result.getMsg());
        }
    }
}
